﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

using Continental.Code.Entity;
using Continental.Code.DAL;

namespace Continental.Code.BL
{
    public class ExtensionProvider
    {
        #region Quản trị Quận Huyện
        public static void InserQuanHuyen(QuanHuyen theQuanHuyen)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_InsertQuanHuyen";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@TitleOrName", theQuanHuyen.TitleOrName));
            command.Parameters.Add(new SqlParameter("@NumberOfOrder", theQuanHuyen.NumberOfOrder));
            DataProvider.ExecuteNoneQuery(command);
        }

        public static void UpdateQuanHuyenByID(QuanHuyen theQuanHuyen)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_UpdateQuanHuyenByID";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@QuanHuyenID", theQuanHuyen.QuanHuyenID));
            command.Parameters.Add(new SqlParameter("@TitleOrName", theQuanHuyen.TitleOrName));
            command.Parameters.Add(new SqlParameter("@NumberOfOrder", theQuanHuyen.NumberOfOrder));
            DataProvider.ExecuteNoneQuery(command);
        }

        public static void DeleteQuanHuyenByID(Int32 quanHuyenID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_DeleteQuanHuyenByID";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@QuanHuyenID", quanHuyenID));
            DataProvider.ExecuteNoneQuery(command);
        }

        public static QuanHuyen GetQuanHuyenByID(Int32 quanHuyenID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetQuanHuyenByID";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@QuanHuyenID", quanHuyenID));
            DataSet dsResult = DataProvider.ExecuteDataSet(command);
            QuanHuyen theQuanHuyen = null;

            if (dsResult.Tables[0].Rows.Count > 0)
            {
                theQuanHuyen = new QuanHuyen();
                theQuanHuyen.QuanHuyenID = Int32.Parse(dsResult.Tables[0].Rows[0]["QuanHuyenID"].ToString());
                theQuanHuyen.TitleOrName = dsResult.Tables[0].Rows[0]["TitleOrName"].ToString();
                theQuanHuyen.NumberOfOrder = Int32.Parse(dsResult.Tables[0].Rows[0]["NumberOfOrder"].ToString());
                theQuanHuyen.DeletedFlag = Boolean.Parse(dsResult.Tables[0].Rows[0]["DeletedFlag"].ToString());
                theQuanHuyen.CityID = Int32.Parse(dsResult.Tables[0].Rows[0]["CityID"].ToString());
                theQuanHuyen.AmountOfTuDiem = Int32.Parse(dsResult.Tables[0].Rows[0]["AmountOfTuDiem"].ToString());
            }

            return theQuanHuyen;
        }

        public static DataSet GetQuanHuyenList()
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetQuanHuyenList";
            command.CommandType = CommandType.StoredProcedure;
            DataSet dsResult = DataProvider.ExecuteDataSet(command);
            return dsResult;
        }

        #endregion

        #region Quản trị Đường Phố
        public static void InserDuongPho(DuongPho theDuongPho, out Int32 duongPhoID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_InsertDuongPho";
            command.CommandType = CommandType.StoredProcedure;
            SqlParameter duongPhoIDParam = new SqlParameter("@DuongPhoID", SqlDbType.Int);
            duongPhoIDParam.Direction = ParameterDirection.Output;
            command.Parameters.Add(duongPhoIDParam);
            command.Parameters.Add(new SqlParameter("@TitleOrName", theDuongPho.TitleOrName));
            command.Parameters.Add(new SqlParameter("@NumberOfOrder", theDuongPho.NumberOfOrder));
            DataProvider.ExecuteNoneQuery(command);
            duongPhoID = Int32.Parse(command.Parameters["@DuongPhoID"].Value.ToString());
        }

        public static void UpdateDuongPhoByID(DuongPho theDuongPho)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_UpdateDuongPhoByID";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@DuongPhoID", theDuongPho.DuongPhoID));
            command.Parameters.Add(new SqlParameter("@TitleOrName", theDuongPho.TitleOrName));
            command.Parameters.Add(new SqlParameter("@NumberOfOrder", theDuongPho.NumberOfOrder));
            DataProvider.ExecuteNoneQuery(command);
        }

        public static void DeleteDuongPhoByID(Int32 duongPhoID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_DeleteDuongPhoByID";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@DuongPhoID", duongPhoID));
            DataProvider.ExecuteNoneQuery(command);
        }

        public static void DeleteDuongPhoQuanHuyenByDuongPhoID(Int32 duongPhoID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_DeleteDuongPhoQuanHuyenByDuongPhoID";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@DuongPhoID", duongPhoID));
            DataProvider.ExecuteNoneQuery(command);
        }

        public static void InserDuongPhoQuanHuyen(Int32 duongPhoID, Int32 quanHuyenID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_InsertDuongPhoQuanHuyen";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@DuongPhoID", duongPhoID));
            command.Parameters.Add(new SqlParameter("@QuanHuyenID", quanHuyenID));
            DataProvider.ExecuteNoneQuery(command);
        }

        public static DataSet GetDuongPhoQuanHuyenByDuongPhoID(Int32 duongPhoID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetDuongPhoQuanHuyenByDuongPhoID";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@DuongPhoID", duongPhoID));
            return DataProvider.ExecuteDataSet(command);
        }

        public static DuongPho GetDuongPhoByID(Int32 duongPhoID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetDuongPhoByID";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@DuongPhoID", duongPhoID));
            DataSet dsResult = DataProvider.ExecuteDataSet(command);
            DuongPho theDuongPho = null;

            if (dsResult.Tables[0].Rows.Count > 0)
            {
                theDuongPho = new DuongPho();
                theDuongPho.DuongPhoID = Int32.Parse(dsResult.Tables[0].Rows[0]["DuongPhoID"].ToString());
                theDuongPho.TitleOrName = dsResult.Tables[0].Rows[0]["TitleOrName"].ToString();
                theDuongPho.NumberOfOrder = Int32.Parse(dsResult.Tables[0].Rows[0]["NumberOfOrder"].ToString());
                theDuongPho.DeletedFlag = Boolean.Parse(dsResult.Tables[0].Rows[0]["DeletedFlag"].ToString());
                theDuongPho.CityID = Int32.Parse(dsResult.Tables[0].Rows[0]["CityID"].ToString());
                theDuongPho.AmountOfTuDiem = Int32.Parse(dsResult.Tables[0].Rows[0]["AmountOfTuDiem"].ToString());
            }

            return theDuongPho;
        }

        public static DataSet GetDuongPhoList(Int32 quanHuyenID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetDuongPhoList";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@QuanHuyenID", quanHuyenID));
            DataSet dsResult = DataProvider.ExecuteDataSet(command);
            return dsResult;
        }

        #endregion
    }
}
